package com.bookmanger.config;

import com.bookmanger.common.intercepter.LoginInterceptor;
import com.bookmanger.common.intercepter.RefreshTokenInterceptor;
import com.bookmanger.common.intercepter.UserLoginInterceptor;
import com.bookmanger.common.intercepter.UserRefreshTokenInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * @author huangfuhao
 * @Date 2024/4/23
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //  管理员登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns(
                        "/api/manager/**"
                )
                .excludePathPatterns(
                        "/api/manager/login"
                )
                .order(1);


        //  管理员token刷新的拦截器（判断管理权限）
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns(
                        "/api/manager/**",
                        "/api/bookBorrow/restore", //还书
                        "/api/bookBorrow/OrderList", //管理员获取用户借阅列表
                        "/api/bookSearch/GroundBook", //上架书籍
                        "/api/bookSearch/unGroundBook", //下架书籍
                        "/api/bookSearch/addBook", //添加书籍
                        "/api/bookSearch/updateBook", //修改书籍
                        "/api/bookSearch/deleteBook", //删除书籍
                        "/api/userManager/userDisable", //停用账号
                        "/api/userManager/userFreeze", //冬结账号
                        "/api/userManager/userEnable", //启用账号
                        "/api/userManager/userList" //查询用户列表
                ).order(0);


        //  小程序用户登录拦截器
        registry.addInterceptor(new UserLoginInterceptor())
                .addPathPatterns(
                        "/api/bookBorrow/**",
                        "/api/bookSearch/**",
                        "/api/userCenter/**"
                )
                .excludePathPatterns(
                        "/api/manager/**",
                        "/api/bookBorrow/restore", //还书
                        "/api/bookBorrow/OrderList", //管理员获取用户借阅列表
                        "/api/bookSearch/GroundBook", //上架书籍
                        "/api/bookSearch/unGroundBook", //下架书籍
                        "/api/bookSearch/addBook", //添加书籍
                        "/api/bookSearch/updateBook", //修改书籍
                        "/api/bookSearch/deleteBook", //删除书籍
                        "/api/userManager/userDisable", //停用账号
                        "/api/userManager/userFreeze", //冬结账号
                        "/api/userManager/userEnable", //启用账号
                        "/api/bookSearch/getBookCount", //获取图书数量
                        "/api/bookBorrow/getOrderCount", //获取借阅总数
                        "/api/userCenter/getUserCount", //获取用户总数
                        "/api/bookBorrow/rank", //获取借阅排行榜
                        "/api/bookSearch/getBookCategoryCount", //获取每种分类的书籍数量
                        "/api/bookBorrow/getOrderBookCategoryCount", //获取每种分类的借阅书籍数量
                        "/api/bookBorrow/passBorrow",//管理员同意借阅预约
                        "/api/bookBorrow/passRestore",//管理员同意归还图书
                        "/api/bookBorrow/refuseRestore",//管理员拒绝归还图书
                        "/api/bookBorrow/orders",//
                        "/api/bookSearch/getBookList"//管理员拒绝归还图书
                )
                .order(1);


        //  小程序用户token刷新的拦截器
        registry.addInterceptor(new UserRefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns(
                        "/api/bookBorrow/**",
                        "/api/bookSearch/**",
                        "/api/userCenter/**"
                ).excludePathPatterns(
                        "/api/bookSearch/updateBook" //修改书籍
        )
                .order(0);
//

    }


}
